﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "CmdCreateRaftFoundation.h"
#include "StructureInstanceCmdUtils.h"
#include "StructureProfileInstanceInput.h"
#include "GmUiStructureCommandRegister.h"
#include "StructureCmdIds.h"
#include "StructureInstancePresetParameterUtils.h"
#include "StructureDefaultFamilyNames.h"
#include "GbmpBuiltInCategoryUniIdentitiesNew.h"
#include "GbmpModelViewTypes.h"
#include "IStructureRaftFoundation.h"
#include "IInstanceType.h"
#include "UiCommonDialog.h"
#include "IFamily.h"
#include "IUiView.h"
#include "IParameterDefinitionLibrary.h"
#include "IModelView.h"
#include "IEditMode.h"
#include "IModelLine.h"
#include "ILine3d.h"
#include "IStructureRaftFoundationEditModeBehavior.h"
#include "StructureEditModeTypes.h"
#include "ProfileUsedModelLineUtils.h"
#include "IElementStatus.h"
#include "UiDocumentViewUtils.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;
REGISTER_GM_COMMAND(CmdCreateRaftFoundation)

gcmp::CmdCreateRaftFoundation::CmdCreateRaftFoundation()
    : CmdCreateStructurePlateBase(ID_CMD_CREATE_RAFT_FOUNDATION)
{

}

gcmp::UniIdentity gcmp::CmdCreateRaftFoundation::GetCategory() const
{
    UniIdentity categoryUid = BuiltInCategoryUniIdentities::BICU_STRUCTURE_FOUNDATION;
    return categoryUid;
}

gcmp::StructureFamilyUiUtils::FamilyCreators gcmp::CmdCreateRaftFoundation::GetDefaultFamilyCreators() const
{
    StructureFamilyUiUtils::FamilyCreators defaultFamilyCreators
    {
        { GetDefaultFamilyName(), BuiltInStructureFamilies::CreateBuiltInRaftFoundationFamilyDocument }
    };

    return defaultFamilyCreators;
}

std::wstring gcmp::CmdCreateRaftFoundation::GetDefaultFamilyName() const
{
    return DEFAULT_STRUCTURE_RAFT_FOUNDATION_FAMILY_NAME;
}

gcmp::ElementId gcmp::CmdCreateRaftFoundation::CreateTempPresetElement() const
{
    IDocument* pDoc = UiDocumentViewUtils::GetCurrentDocument();
    DBG_WARN_AND_RETURN_UNLESS(pDoc, ElementId::InvalidID, L"pDoc为空",L"GDMPLab",L"2024-03-30");

    IModelView* pModelView = UiDocumentViewUtils::GetCurrentModelView();
    DBG_WARN_AND_RETURN_UNLESS(pModelView, ElementId::InvalidID, L"pUiView为空",L"GDMPLab",L"2024-03-30");

    // 创建三条模型线，画临时对象的轮廓
    OwnerPtr<ILine3d> opLine3d[3];
    IModelLine* pIModelLine[3];
    std::vector<IModelLine*> profileCurves;

    opLine3d[0] = ILine3d::Create(Vector3d(0.0, 0.0, 0.0), Vector3d(100.0, 0.0, 0.0));
    opLine3d[1] = ILine3d::Create(Vector3d(100.0, 0.0, 0.0), Vector3d(0.0, 100.0, 0.0));
    opLine3d[2] = ILine3d::Create(Vector3d(0.0, 100.0, 0.0), Vector3d(0.0, 0.0, 0.0));

    for (int i = 0; i < 3; i++)
    {
        pIModelLine[i] = ProfileUsedModelLineUtils::CreateModelLineUsedForProfile(pDoc, TransferOwnership(opLine3d[i]));
        pIModelLine[i]->GetOwnerElement()->GetStatus()->SetIsVisible(false);      // 标记临时对象的模型线不可见
        profileCurves.push_back(pIModelLine[i]);
    }

    //构造InstanceInput
    OwnerPtr<StructureRaftFoundationElementInput> opInput = TransferOwnershipCast<StructureRaftFoundationElementInput>(GetInstanceInput());
    opInput->CreationOption = ElementCreationOptions::Normal;
    opInput->CanBeShared = false;
    opInput->Visible = false;
    opInput->ProfileCurves = profileCurves;

    // 根据上述模型线创建instance
    IStructureRaftFoundation* pRaftFoundation = IStructureRaftFoundation::Create(opInput.get());
    DBG_WARN_AND_RETURN_UNLESS(pRaftFoundation, ElementId::InvalidID, L"结构筏板创建失败",L"GDMPLab",L"2024-03-30");

    return pRaftFoundation->GetElementId();
}

bool gcmp::CmdCreateRaftFoundation::CheckRunTimeEnvironment() const
{
    IDocument* pDoc = UiDocumentViewUtils::GetCurrentDocument();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDoc, L"pDoc为空",L"GDMPLab",L"2024-03-30");

    IModelView* pModelView = UiDocumentViewUtils::GetCurrentModelView();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pModelView, L"pModelView为空",L"GDMPLab",L"2024-03-30");

    const std::wstring& viewType = pModelView->GetUserDataId();

    if (PlanViewType != viewType && View3DViewType != viewType)
    {
        UiCommonDialog::ShowMessageBox(GBMP_TR(L"绘制筏板基础"), GBMP_TR(L"此视图下不支持绘制筏板基础"), (int)UiCommonDialog::ButtonType::OK);
        return false;
    }
    IEditMode* pTopEditMode = IEditMode::GetTopActiveEditMode(pDoc);
    if (pTopEditMode != nullptr)
    {
        UiCommonDialog::ShowMessageBox(GBMP_TR(L"警告"), GBMP_TR(L"正在编辑筏板基础！不能重新创建实例"), (int)UiCommonDialog::ButtonType::OK);
        return false;
    }
    return true;
}

gcmp::OwnerPtr<gcmp::StructureProfileInstanceElementInput> gcmp::CmdCreateRaftFoundation::GetInstanceInput() const
{
    IDocument* pDoc = UiDocumentViewUtils::GetCurrentDocument();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDoc, L"pDoc为空",L"GDMPLab",L"2024-03-30");

    IUiView* pUIView = UiDocumentViewUtils::GetCurrentUiView();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pUIView, L"pUIView为空",L"GDMPLab",L"2024-03-30");

    OwnerPtr<StructureRaftFoundationElementInput> raftFoundationInput = NEW_AS_OWNER_PTR(StructureRaftFoundationElementInput);
    bool bOk = StructureInstanceCmdUtils::GenerateRaftFoundationInput(pDoc, pUIView->GetModelView(), this->GetCategory(), this->GetCategorySecondKey(), m_familyId, m_instanceTypeId, *raftFoundationInput);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(bOk, L"GenerateStructureFloorInput失败",L"GDMPLab",L"2024-03-30");

    return TransferOwnership(raftFoundationInput);
}

const std::wstring CmdCreateRaftFoundation::GetInitialCommandId() const
{
    return ID_CMD_STRUCTURE_DRAW_STRAIGHT_PROFILE;
}

IEditMode* CmdCreateRaftFoundation::PrepareEditMode(IDocument* pDoc) const
{
    IEditMode* pProfileEditMode = IEditMode::Get(pDoc, StructureEditModeTypes::StructureRaftFoundationEditModeType);
    if (!pProfileEditMode)
    {
        pProfileEditMode = IEditMode::Create(pDoc, IStructureRaftFoundationEditModeBehavior::Create());
    }
    return pProfileEditMode;
}
